package pl.smath.renderer.model;

import gwt.g2d.client.graphics.Surface;
import pl.smath.expression.model.presentation.FactorofTerm.RelationType;
import pl.smath.renderer.model.sign.FactorofSign;
import pl.smath.renderer.renderer.TermRendererException;
import pl.smath.renderer.utils.Cursor;
import pl.smath.renderer.utils.InteractionSocket;

import com.google.gwt.user.client.ui.RootPanel;

public class FactorofView extends View {

	private View left;
	private View right;
	private RelationType type;
	
	private FactorofSign factorofSign;
	//private FactorofSign factorofSign;
	
	public FactorofView(RelationType type, View left, View right) {
		super("|");
		this.left = left;
		this.right = right;
		this.type = type;
		factorofSign = new FactorofSign(type == RelationType.FACTOR_OF);
	}

	/**
	 * @return the left
	 */
	public View getLeft() {
		return left;
	}


	/**
	 * @param left the left to set
	 */
	public void setLeft(View left) {
		this.left = left;
	}


	/**
	 * @return the right
	 */
	public View getRight() {
		return right;
	}


	/**
	 * @param right the right to set
	 */
	public void setRight(View right) {
		this.right = right;
	}


	/**
	 * @return the type
	 */
	public RelationType getType() {
		return type;
	}


	/**
	 * @param type the type to set
	 */
	public void setType(RelationType type) {
		this.type = type;
	}


	/**
	 * render text at the current position
	 * @param canvas
	 */
	@Override
	public void render(Surface canvas, InteractionSocket socket) throws TermRendererException {
		//TODO brackets
		Surface surface = new Surface();
		surface.setFont(this.getFont());
		Cursor cursor = this.getCursor();
		
		if ((this.getFont() == null)||(this.getFontHeight() == null)) {
			this.setFont(canvas.getFont());
		}
		canvas.setFont(this.getFont());
		
		this.left.render(canvas, socket);
		this.right.render(canvas, socket);
		
		factorofSign.render(canvas, new Cursor(cursor.getX() + left.getWidth(), cursor.getY()));
		
		
	}

	/**
	 * set the position on canvas to render
	 * @param Cursor
	 */
	@Override
	public void setCursor(Cursor cursor) {
		cursor.setY(cursor.getY());
		super.setCursor(new Cursor(cursor.getX(), cursor.getY()));
		
		Surface surface = new Surface();
		RootPanel.get().add(surface);
		surface.setFont(this.getFont());

		Double artifactWidth = 0d;
		
		artifactWidth = factorofSign.getWidth();
	
		Double minusWidth = 0d;
		
		if (this.isNegative()) 
			minusWidth = this.minusSign.getWidth();
		
		if (this.isNegative()) {
			cursor.setX(cursor.getX() + minusWidth);
		}
		
		left.setCursor(new Cursor(cursor.getX(), cursor.getY()));
		right.setCursor(new Cursor(cursor.getX() + left.getWidth() + artifactWidth, cursor.getY()));

		RootPanel.get().remove(surface);
	}
	
	/**
	 * @param font the font to set
	 */
	@Override
	public void setFont(String font) {
		this.left.setFont(font);
		this.right.setFont(font);

		if (font.indexOf("px")!=-1) {
			this.setFontHeight(new Double(font.substring(0, font.indexOf("px"))));
			this.font = font.substring(font.indexOf("px ")+3);
			this.minusSign.setFont(this.font, this.getFontHeight());
			this.factorofSign.setFont(font, getFontHeight());
			update();
		} else {
			this.setFontHeight(null);
			this.font = null;
		}

	}
	
	/**
	 * updates the view after changes
	 */
	@Override
	public void update() {
		Surface surface = new Surface();
		RootPanel.get().add(surface);
		surface.setFont(this.getFont());

		Double artifactsWidth = 0d;
		
		artifactsWidth = factorofSign.getWidth();
		
		surface.setFont(this.getFont());
		
		Double minusWidth = 0d;
		
		if (isNegative()) 
			minusWidth = this.minusSign.getWidth();
		
		this.setWidth(minusWidth + left.getWidth() + right.getWidth() + artifactsWidth);
		this.setHeight(Math.max(left.getHeight(), right.getHeight()));

		RootPanel.get().remove(surface);
	}
}
